<template>
   <div id="app">
        <form @submit.prevent="diagnose">
            姓名：<input type="text" v-model="patient.name"><br>
            年龄：<input type="number" v-model.number="patient.age"><br>
            身高：<input type="number" step="0.01" v-model.number="patient.height"><br>
            体重：<input type="number" step="0.1" v-model.number="patient.weight"><br>
            病史：<input type="text" v-model="patient.medicalHistory"><br>
            <button type="submit">诊断</button>
        </form>

        <p>患者 {{ patient.name }} 的诊断结果：{{ diagnosisResult }}</p>
    </div>

</template>

<script>

export default {
  
data() {
  return{
    patient: {
                    name: '',
                    age: null,
                    height: null,
                    weight: null,
                    medicalHistory: ''
                },
                diagnosisResult: '未诊断'
  }
                
            },
            methods: {
                diagnose() {
                    if (!this.patient.height || !this.patient.weight) {
                        this.diagnosisResult = '请填写身高和体重';
                        return;
                    }

                    const bmi = this.patient.weight / (this.patient.height * this.patient.height);
                    let result;

                    if (bmi < 18.5) {
                        result = '体重过轻';
                    } else if (bmi >= 18.5 && bmi <= 24.9) {
                        result = '正常范围';
                    } else if (bmi >= 25 && bmi <= 29.9) {
                        result = '超重';
                    } else {
                        result = '肥胖';
                    }

                    this.diagnosisResult = `${result} (${bmi.toFixed(1)})`;
                }
            }
}

</script>

<style>

</style>